﻿using System;
using System.Collections.Generic;
using System.Data;
using System.Linq;
using System.Text;
using CRMFramework;

namespace CRMBiz
{
   public static class ReferrerHandler
    {
       public static DataSet GetReferrerList(int pageIndex, int pageSize)
       {
           DataSet ds;
           try
           {
               var sbSql = new StringBuilder("SELECT *,(select count(*) from crm_referrer) as Total from crm_referrer order by ReferrerID");
               ds = DBhelper.ExecuteDataset(sbSql.ToString());
           }
           catch (Exception ex)
           {
               throw ex;
           }
           return ds;
       }

       public static int AddReferrer(string Referrer)
       {
           int intReturnValue = 0;
           try
           {
               var para = new[] {
                    DBhelper.SetParameter("@Referrer", Referrer),
                };

               string sbSql = "Select * From crm_referrer Where Referrer = @Referrer";
               DataSet ds = DBhelper.ExecuteDataset(sbSql, para);
               if (ds.Tables[0].Rows.Count > 0)
               {
                   intReturnValue = 0;
               }
               else
               {
                   sbSql = "insert into crm_referrer(Referrer) values(@Referrer)";
                   DBhelper.ExecuteNonQuery(sbSql, para);
                   intReturnValue = 1;
               }
           }
           catch (Exception ex)
           {
               throw ex;
           }
           return intReturnValue;
       }

       public static int EditReferrer(int ReferrerID, string Referrer)
       {
           int intReturnValue = 0;
           try
           {
               var para = new[] {
                    DBhelper.SetParameter("@ReferrerID", ReferrerID),
                    DBhelper.SetParameter("@Referrer", Referrer),
                };

               string sbSql = "Select * From crm_referrer Where ReferrerID <> @ReferrerID And  Referrer = @Referrer";
               DataSet ds = DBhelper.ExecuteDataset(sbSql, para);
               if (ds.Tables[0].Rows.Count > 0)
               {
                   intReturnValue = 0;
               }
               else
               {
                   sbSql = "update crm_referrer set Referrer = @Referrer where ReferrerID = @ReferrerID";
                   DBhelper.ExecuteNonQuery(sbSql, para);
                   intReturnValue = 1;
               }
           }
           catch (Exception ex)
           {
               throw ex;
           }
           return intReturnValue;
       }

       public static bool DeleteReferrer(int ReferrerID)
       {
           bool flag = false;
           try
           {
               var para = new[] {
                    DBhelper.SetParameter("@ReferrerID", ReferrerID),
                };

               string sbSql = "delete from crm_referrer where ReferrerID=@ReferrerID";
               DBhelper.ExecuteNonQuery(sbSql, para);
               flag = true;
           }
           catch (Exception ex)
           {
               throw ex;
           }
           return flag;
       }
    }
}
